package com.rockcent.service.impl;

import com.rockcent.common.common.BusinessException;
import com.rockcent.mall.lib.domain.MerchantFund;
import com.rockcent.mall.lib.domain.MerchantFundFlow;
import com.rockcent.mall.lib.domain.enumclass.FundTransactionFlow;
import com.rockcent.mall.lib.domain.enumclass.MerchantFundFlowTransactionType;
import com.rockcent.mall.lib.domain.enumclass.MerchantPayType;
import com.rockcent.mall.lib.repository.MerchantFundFlowRepository;
import com.rockcent.mall.lib.repository.MerchantFundRepository;
import com.rockcent.mall.lib.repository.MerchantRepository;
import com.rockcent.service.MerchantFundFlowSrv;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 商户资金流水
 * Created by zhangjh on 2017/3/29.
 */
@Service
public class MerchantFundFlowSrvImpl implements MerchantFundFlowSrv {


    @Autowired
    private MerchantFundFlowRepository merchantFundFlowRepository;


    /**
     * @param fund
     */
    @Override
    public void save(MerchantFund fund, BigDecimal operationAmount, MerchantPayType payType, String orderNo, FundTransactionFlow transactionFlow, MerchantFundFlowTransactionType transactionType, String remark) {

        if (null == fund) {
            throw new BusinessException("信息错误");
        }


        Long merchantId = fund.getMerchantId();
        Long mallId = fund.getMallId();


        if (null == operationAmount) {
            throw new BusinessException("金额为空");
        }


        MerchantFundFlow merchantFundFlow = new MerchantFundFlow();


        BigDecimal merchantFundFrozen = fund.getFrozen();
        BigDecimal merchantFundAvailableAmount = fund.getAvailableAmount();
        BigDecimal merchantFundTotalAmount = fund.getTotalAmount();
        BigDecimal availableAmount = merchantFundAvailableAmount;
        BigDecimal totalAmount = merchantFundTotalAmount;

        merchantFundFlow.setFrozen(merchantFundFrozen);
        merchantFundFlow.setTotalAmount(totalAmount);
        merchantFundFlow.setAvailableAmount(availableAmount);
        merchantFundFlow.setFrozen(merchantFundFrozen);
        merchantFundFlow.setMerchantFundId(fund.getId());
        merchantFundFlow.setMallId(mallId);
        merchantFundFlow.setMerchantId(merchantId);
        merchantFundFlow.setPayType(payType);
        merchantFundFlow.setTransactionType(transactionType);
        merchantFundFlow.setTransactionFlow(transactionFlow);
        merchantFundFlow.setOperationAmount(operationAmount);
        merchantFundFlow.setMerchantOrderNum(orderNo);
        merchantFundFlow.setRemark(remark);

        merchantFundFlow = merchantFundFlowRepository.save(merchantFundFlow);
        merchantFundFlow.setParentId(merchantFundFlow.getId());
        merchantFundFlowRepository.save(merchantFundFlow);

    }


}
